package com.tgpms.web.element.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tgpms.common.PageUtils;
import com.tgpms.common.Query;
import com.tgpms.util.Common;
import com.tgpms.web.element.dao.WsDocDao;
import com.tgpms.web.element.dao.WsDocRelDao;
import com.tgpms.web.element.entity.WsDoc;
import com.tgpms.web.element.entity.WsDocRel;
import com.tgpms.web.element.service.WsDocRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Service("wsDocRelService")
public class WsDocRelServiceImpl extends ServiceImpl<WsDocRelDao, WsDocRel> implements WsDocRelService {
    @Value("${ftpparam.wzyslocalloadfile}")
    private String wzyslocalloadfile;
    @Value("${ftpparam.filepath4}")
    private String filepath4;
    @Autowired
    private WsDocDao wsDocDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WsDocRel> page = this.page(
                new Query<WsDocRel>().getPage(params),
                // 拼接查询条件
                new LambdaQueryWrapper<WsDocRel>()
        );

        return new PageUtils(page);
    }

    @Override
    public void importFile(MultipartFile file, String docRelId, String docRelTable,String insertName,String fileName) throws Exception {
        //指定存放上传文件的临时目录
        String fileDir = wzyslocalloadfile;
        File dir = new File(fileDir);
        //判断目录是否存在，不存在则创建目录
        if (!dir.exists()){
            dir.mkdirs();
        }
        //将上传文件转文件流
        // 处理中文文件名乱码
         fileName = Common.getUTFStringByEncoding(fileName);
        // 获取文件后缀
        String suffix=(fileName.substring(fileName.lastIndexOf(".")+1)).toLowerCase();
        String newFileName = UUID.randomUUID().toString()+"."+suffix;
        File excelFile = new File(dir.getAbsolutePath()+ File.separator+ newFileName);
        file.transferTo(excelFile);

        String id = Common.fromDateFormats() + UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
        WsDocRel wsDocRel = new WsDocRel();
        wsDocRel.setId(id);
        wsDocRel.setDocRelId(docRelId);
        wsDocRel.setDocRelTable(docRelTable);
        int insert = this.baseMapper.insert(wsDocRel);
        if (insert > 0) {
            WsDoc wsDoc = new WsDoc();
            wsDoc.setId(id);
            wsDoc.setOriginalName(fileName);
            wsDoc.setNewName(newFileName);
            wsDoc.setExtName(suffix);
            wsDoc.setFilePath(filepath4);
            wsDoc.setFileSize(String.valueOf(file.getSize()));
            wsDoc.setUploadUser(insertName);
            wsDoc.setUploadDate(new Date());
            wsDocDao.insert(wsDoc);
        }
    }

}